<template>
	<div class="app_home">
		<EmptyLayout v-if="pageStatus === 2" description="服务异常" />
	</div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import EmptyLayout from '@C/EmptyLayout/EmptyLayout'

// 客户id
const custId = bjsdk.query('custId')
// 通过授权获取的客户id
const contactId = bjsdk.query('contactId')

const pageStatus = ref(0) // 0初始化 1:获取用户id成功（跳转到详情页） 2:获取用户id失败，展示报错

const getContactCustId = () => {
	if (!bjsdk?.isWW || !bjsdk?.gContext) {
		return Promise.resolve('')
	}
	return new Promise((resolve) => {
		//判断是否是侧边栏
		bjsdk.gContext((res) => {
			if (
				![
					'contact_profile',
					'single_chat_tools',
					'group_chat_tools',
				].includes(res?.type)
			) {
				resolve('')
			}
			bjsdk.call('invoke', 'getCurExternalContact', {}, ({ userId }) => {
				resolve(userId || '')
			})
		})
	})
}
onMounted(async () => {
	// 判断是否是侧边栏
	if (custId || contactId) {
		bjsdk.goTo('/detail', {
			append: {
				[contactId ? 'contactId' : 'custId']: contactId || custId,
			},
			replace: true,
		})
		return
	}

	const contactCustId = await getContactCustId()
	if (!contactCustId) {
		pageStatus.value = 2
		return
	}
	pageStatus.value = 1
	bjsdk.goTo('/detail', {
		append: {
			contactId: contactCustId,
		},
		replace: true,
	})
})
</script>
